package top.wangjiu.java.jvm.constantinfo.literal;

import top.wangjiu.java.jvm.constantinfo.ConstantPoolInfo;
import top.wangjiu.java.jvm.constantinfo.LiteralConstantPoolInfo;

import java.util.Arrays;

/**
 * @ClassName MethodrefInfo
 * @Description TODO
 * @Author wangjiu
 * @Date 2022/1/14 11:56 上午
 */
public class DoubleCPInfo extends LiteralConstantPoolInfo {

    public final static short TAG = 6;
    public final static short INFO_LENGTH = 8;

    private final short index;

    private final byte[] highBytes;

    private final byte[] lowBytes;

    private final double value;

    public DoubleCPInfo(byte[] bytes, short index){
        this.index = index;
        this.highBytes = Arrays.copyOfRange(bytes,0,4);
        this.lowBytes = Arrays.copyOfRange(bytes,4,8);
        long base = 0;
        for (int i = 0;i< bytes.length;i++){
            base = base<<8 | bytes[i];
        }
        this.value = Double.longBitsToDouble(base);
    }

    @Override
    public short tag() {
        return TAG;
    }

    @Override
    public short index() {
        return index;
    }

    @Override
    public short infoLength() {
        return INFO_LENGTH;
    }

    @Override
    public String toFullString(ConstantPoolInfo[] poolInfos) {
        return "#"+index +" = Double" + "\t\t\t"+value;
    }

    @Override
    public String toString(ConstantPoolInfo[] poolInfos) {
        return Double.toString(value);
    }
}
